import request from '@/utils/request'
import type { ILoginSubmitReq, IUserInfoResp } from '@/layouts/user-layout/type'
import type { IAllUserResp, ICreateUserReq, IDisableUserReq, IUpdateUserReq } from '@/views/accouts/type'

export async function login(loginInfo: ILoginSubmitReq) {
  /**
   *  API.BaseResponseType
   *  code: number
   *  message: string
   *  data: T 这里的T代表着data返回的响应数据类型-对应就是request传入的T
   */
  return await request<{
    accessToken: string
    refreshToken: string
    userInfo: IUserInfoResp
  }>({
    url: 'user/login',
    method: 'post',
    data: loginInfo,
  })
}

export async function userLogout() {
  return await request({
    url: 'user/logout',
    method: 'get',
  })
}

export async function refreshUserToken(params: { refresh_token: string }) {
  return await request({
    url: 'user/refresh',
    method: 'get',
    params,
  })
}

export async function getUserInfo() {
  return await request<IUserInfoResp>({
    url: 'user/info',
    method: 'get',
  })
}

export async function getAllUsers(params: any = { pageNo: 1, pageSize: 10 }): Promise<IAllUserResp> {
  return await request({
    url: 'user/list',
    method: 'get',
    params,
  }).then((res: any) => {
    return {
      data: res.data.users,
      totalCount: res.data.totalCount,
    }
  })
}

export async function createUser(info: ICreateUserReq) {
  return await request({
    url: 'user/register',
    method: 'post',
    data: info,
  })
}

export async function updateUser(info?: IUpdateUserReq) {
  return await request({
    url: 'user/update',
    method: 'post',
    data: info,
  })
}

export async function disableUser(info: IDisableUserReq) {
  return await request({
    url: 'user/freeze',
    method: 'post',
    data: info,
  })
}

export async function sendUpdateInfoCaptcha(params: { address: string }) {
  return await request({
    url: 'user/update/captcha',
    method: 'get',
    params,
  })
}

export async function sendRegisterCaptcha(params: { address: string }) {
  return await request({
    url: 'user/register-captcha',
    method: 'get',
    params,
  })
}

export async function loginHistory(params?: { pageNo: number, pageSize: number, username: string }) {
  return await request({
    url: 'user/login-hisitory',
    method: 'get',
    params,
  }).then((res: any) => {
    return {
      data: res.data.loginList,
      totalCount: res.data.totalCount,
    }
  })
}
// 缓存数据(基本不会更改的数据)减少请求
// const argsMap = {}
// export function getCaseArgs(case) {
//   // 循环播放场景(没有通过axios请求拿到的数据 需要用promise.resolve()包装 调用的时候好统一处理)
//   if (case.jsonArgs)
//     return Promise.resolve(case.jsonArgs)
//   const jsonArgs = argsMap[case.id]
//   if (jsonArgs)
//     return Promise.resolve(jsonArgs)
//   return get('/api/getCaseInfo', {
//     mid,
//   }).then((result) => {
//     const jsonArgs = result ? result.jsonArgs : '该案例暂时无法获取参数'
//     argsMap[result.id] = jsonArgs
//     return jsonArgs
//   })
// }
